/**
 * 添加或者修改页面
 */
var OutOrderDetailedInfoDlg = {
    data: {
        detailedGoodscode: "",
        detailedGoodsId: "",
        detailedGoodsname: "",
        goodsNo: "",

        detailedWeight: "",
        detailedVolume: "",
        detailedPrice: "",

        detailedPacking: "",
        detailedLargeclass: "",
        detailedGoodstype: "",
        goodsUnit: "",
        goodsSpecs: "",
    }
};

layui.use(['form', 'admin', 'ax', 'laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    //让当前iframe弹层高度适应
    admin.iframeAuto();

    //时间渲染---生产日期
    //选择生产日期后，填入批次 
    laydate.render({
        elem:'#detailedDate',
    	/*done: function(value, date, endDate){
      	    var detailedDate = value.replace(/[-]/g,"");//得到日期生成的值，如：2017-08-18,去除-
      	    $("#detailedBatchnumber").val(detailedDate);
      	}*/
    });

    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/outOrderDetailed/addItem", function (data) {
            Feng.success("Add successful!");
            var Index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(Index);
        }, function (data) {
            Feng.error("Add failed!" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;

    });


    //除以的精确结果
    function accDiv(arg1,arg2){
        var t1=0,t2=0,r1,r2;
        try{
            t1=arg1.toString().split(".")[1].length;
        }catch(e){}

        try{
            t2=arg2.toString().split(".")[1].length;
        }catch(e){}

        with(Math){
            r1=Number(arg1.toString().replace(".",""));
            r2=Number(arg2.toString().replace(".",""));
            return (r1/r2)*pow(10,t2-t1);
        }
    }

    //乘以的精确结果
    function accMul(arg1,arg2){
        var m=0,s1=arg1.toString(),
            s2=arg2.toString();
        try{
            m+=s1.split(".")[1].length}catch(e){}
        try{
            m+=s2.split(".")[1].length}catch(e){}
        return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m
        )}

    //点击商品名称
    $("#detailedGoodsname").click(function(){
        layer.open({
            type: 2,
            title: 'Select Product',
            area: ['850px', '500px'],
            content: Feng.ctxPath + '/outOrderDetailed/toChooseGoods?goodsCustomerId=' + $("#outCustomerId").val(),
            end: function () {
                $("#detailedGoodsname").val(OutOrderDetailedInfoDlg.data.detailedGoodsname);//商品名称
                $("#detailedGoodsId").val(OutOrderDetailedInfoDlg.data.detailedGoodsId);//商品id
                $("#detailedGoodscode").val(OutOrderDetailedInfoDlg.data.detailedGoodscode);//商品编码
                $("#goodsNo").val(OutOrderDetailedInfoDlg.data.goodsNo);

                $("#detailedLargeclass").val(OutOrderDetailedInfoDlg.data.detailedLargeclass);//商品大类
                $("#detailedGoodstype").val(OutOrderDetailedInfoDlg.data.detailedGoodstype);//商品类别
                $("#goodsUnit").val(OutOrderDetailedInfoDlg.data.goodsUnit);
                $("#detailedPacking").val(OutOrderDetailedInfoDlg.data.detailedPacking);//包装
                $("#goodsSpecs").val(OutOrderDetailedInfoDlg.data.goodsSpecs);

                $("#detailedWeight").val(OutOrderDetailedInfoDlg.data.detailedWeight);//重量
                $("#detailedVolume").val(OutOrderDetailedInfoDlg.data.detailedVolume);//体积
                $("#detailedPrice").val(OutOrderDetailedInfoDlg.data.detailedPrice);//单价
            }
        });
    });

    //输入大包数量
    $("#detailedPutbalenum").keyup(function(){
        var detailedGoodsName = $("#detailedGoodsname").val();
        if (detailedGoodsName == null||detailedGoodsName == '') {
            layer.msg('Please select a product');
        } else {
            var detailedPutNumber = $("#detailedPutbalenum").val();//大包数量
            var detailedWeight = $("#detailedWeight").val();//重量
            var detailedVolume = $("#detailedVolume").val();//体积
            var detailedPrice = $("#detailedPrice").val();//单价
            $("#detailedNumber").val(detailedPutNumber);//改变应入数量
            $("#detailedOutVolume").val(accMul(detailedPutNumber , detailedVolume));//改变应出体积
            $("#detailedOutWeight").val(accMul(detailedPutNumber , detailedWeight));//改变应出重量
            $("#detailedMoney").val(accMul(detailedPutNumber , detailedPrice));//改变金额
        }
    });

    //输入应入数量
    $("#detailedNumber").keyup(function(){
        var detailedGoodsName = $("#detailedGoodsname").val();
        if (detailedGoodsName == null||detailedGoodsName == '') {
            layer.msg('Please select a product');
        } else {
            var detailedNumber = $("#detailedNumber").val();//应出数量
            var detailedWeight = $("#detailedWeight").val();//重量
            var detailedVolume = $("#detailedVolume").val();//体积
            var detailedPrice = $("#detailedPrice").val();//单价
            $("#detailedPutbalenum").val(detailedNumber);//改变应出数量
            $("#detailedOutVolume").val(accMul(detailedNumber , detailedVolume));//改变应出体积
            $("#detailedOutWeight").val(accMul(detailedNumber , detailedWeight));//改变应出重量
            $("#detailedMoney").val(accMul(detailedNumber , detailedPrice));//改变金额
        }
    });

    //输入单价时
    $("#detailedPrice").keyup(function () {
        var detailedGoodsName = $("#detailedGoodsname").val();
        if (detailedGoodsName == null||detailedGoodsName == '') {
            layer.msg('Please select a product');
        } else {
            var detailedNumber = $("#detailedNumber").val();//应出数量
            var detailedPrice = $("#detailedPrice").val();//单价
            $("#detailedMoney").val(accMul(detailedNumber , detailedPrice));//改变金额
        }
    });

    //输入金额
    $("#detailedMoney").keyup(function(){
        var detailedGoodsName = $("#detailedGoodsname").val();
        if (detailedGoodsName == null||detailedGoodsName == '') {
            layer.msg('Please select a product');
        } else {
            var detailedNumber = $("#detailedNumber").val();//应出数量
            var detailedMoney = $("#detailedMoney").val();//金额
            $("#detailedPrice").val(accDiv(detailedMoney,detailedNumber));//改变单价
        }
    });


    //返回按钮
    $("#backupPage").click(function () {
        var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });

});